
#include "rmfileDispose.h"

void rmfileDispose(int netfd,MYSQL * mysql,char * username,char * spath,char * real_prev){
    char sql[1024] = {0};
    sprintf(sql,"select * from usrfile where username = '%s' and path = '%s';",username,spath);
    
    mysql_query(mysql,sql);

    MYSQL_RES * table = mysql_store_result(mysql);

    if(mysql_num_rows(table) == 0){
        send(netfd,"1",1,MSG_NOSIGNAL);
        return ;
    }
    MYSQL_ROW row1 = mysql_fetch_row(table);
    
    if(strcmp(row1[5],"d") == 0 ){
        send(netfd,"2",1,MSG_NOSIGNAL);
        return ;
    }
    bzero(sql,sizeof(sql)); 
    sprintf(sql,"select * from usrfile where md5 = '%s';",row1[6]);
    mysql_query(mysql,sql);
    
    table = mysql_store_result(mysql);

    if(mysql_num_rows(table) > 1){
        bzero(sql,sizeof(sql));
        sprintf(sql,"delete from usrfile where username = '%s' and path = '%s';",username,spath);
        mysql_query(mysql,sql);
    }else{
        bzero(sql,sizeof(sql));
        sprintf(sql,"delete from usrfile where username = '%s' and path = '%s';",username,spath);
        mysql_query(mysql,sql);
        
        char real_path[1024] = {0};
        memcpy(real_path,real_prev,strlen(real_prev));
        char xiegang[] = "/";
        strcat(real_path,xiegang);
        strcat(real_path,row1[6]);   ///
        unlink(real_path);
    }
    send(netfd,"0",1,MSG_NOSIGNAL);
    return;
}
